import { randomStr } from './util';
import { getOssInfo } from '@/api/utils/index'

const OSS = require("ali-oss");
const cdnUrl = 'https://newjieyin.oss-cn-beijing.aliyuncs.com';
const firstPath = 'violation/'; //定义oss上传bucket下根路径

let client = null;
let ossFileDir = "";
let lastName = "";

/**
 * 
 * 函数接收一个Object; 
 * folder:文件最终路径,最上层路径在本页定义;
 * notRename:是否在上传是进行重名名,大部分情况下需要随机命名,可不传;
 * path:文件名;
 * notFolder:不使用folder;
 * file:文件对象;
 * done();成功回调;
 * fail();失败回调;
 */
const ossUpload = params => {
  if (client) {
    ossFileDir = params.folder ? params.folder + "/" : "";
    lastName = params.notRename ? params.path : new Date().getTime() + randomStr(false, 5) + '.' + params.path.split('.')[params.path.split('.').length - 1];
    client
      .multipartUpload(params.notFolder ? ossFileDir + lastName : firstPath + ossFileDir + lastName, params.file)
      .then(data => {
        let lastPath = data.res.requestUrls[0].split("?uploadId")[0];
        data.res.requestUrls[0] = cdnUrl + lastPath.split('.aliyuncs.com')[1]
        params.done && params.done(data);
      })
      .catch(err => {
        params.fail && params.fail(err);
      });
  } else {

  //   {
  //     // 同步上传进度
  //     // http-request返回的参数中有用onProgress可以同步进度
  //     progress: function (p, checkpoint) {
  //         item.onProgress({ percent: Math.floor(p * 100) });
  //     }
  // }
    getOssInfo()
      .then(res => {
        client = new OSS({
          region: res.data.region,
          accessKeyId: res.data.assumeRoleResponse.body.credentials.accessKeyId,
          accessKeySecret: res.data.assumeRoleResponse.body.credentials.accessKeySecret,
          stsToken: res.data.assumeRoleResponse.body.credentials.securityToken,
          expiration: res.data.assumeRoleResponse.body.credentials.expiration,
          endpoint: res.data.endpoint,
          bucket: res.data.bucket,
          // refreshSTSToken: async () => {
          //   const res2 = getOssInfo()
          //   return {
          //       accessKeyId: res2.data.assumeRoleResponse.body.credentials.accessKeyId,
          //       accessKeySecret: res2.data.assumeRoleResponse.body.credentials.accessKeySecret,
          //       stsToken: res2.data.assumeRoleResponse.body.credentials.securityToken
          //   }
          // },
          // refreshSTSTokenInterval: 300000
        });
        ossFileDir = params.folder ? params.folder + "/" : "";
        lastName = params.notRename ? params.path : new Date().getTime() + randomStr(false, 5) + '.' + params.path.split('.')[params.path.split('.').length - 1];
        client
          .multipartUpload(params.notFolder ? ossFileDir + lastName : firstPath + ossFileDir + lastName, params.file)
          .then(data => {
            let lastPath = data.res.requestUrls[0].split("?uploadId")[0];
            data.res.requestUrls[0] = cdnUrl + lastPath.split('.aliyuncs.com')[1]
            params.done && params.done(data);
          })
          .catch(err => {
            params.fail && params.fail(err);
          });
      })
      .catch(err => {
        window.console.log("获取oss配置信息出错", err);
      });
  }
};

export default ossUpload;